class Request {
	config = {
		showModal: true		
	}
	setConfig(configs) {
		this.config = {
			...this.config,
			...configs
		}
	}
	interceptor = {
		//请求之前拦截	
		request: (cb) => {
			if (typeof cb == 'function') {
				this.requestBeforeFun = cb
			}
		},
		//响应拦截
		response: (cb) => {
			if (typeof cb == 'function') {
				this.responseAfterFun = cb;
			}
		}
	}
	//请求前处理
	requestBeforeFun(config) {
		return config
	}
	//响应拦截
	responseAfterFun(res) {
		return res;
	}
	//错误处理
	requestErrorFun(error) {
		return error
	}
	send(action, functionName, data = {}) {
		let options = {
			name: functionName,
			data: {
				action,
				data
			}
		}
		//请求之前拦截
		let beforeData = this.requestBeforeFun(options)
		if (beforeData === false) {
			return new Promise(() => {});
		}
		uni.showLoading({
		    title: '加载中...'
		});
		return uniCloud.callFunction(options).then(({
			result
		}) => {
			 uni.hideLoading();
			//响应拦截
			result = this.responseAfterFun(result);			
			return Promise.resolve(result)
		}).catch(err => {
			uni.hideLoading();
			if(this.requestErrorFun(err) === false){
				return;
			}			
			return Promise.reject(err)
		})
	}


}
export default new Request;
